layui.use(['form', 'layer', 'table'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#linkList',
        url: '/manage/link/data',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 10,
        limits: [10, 15, 20, 25],
        id: "linkListTable",
        cols: [[
            {field: 'id', title: '编号', align: "left"},
            {
                field: 'webicon', title: '网站LOGO', align: "left", templet: function (d) {
                    return '<img style="width: 32px;height: 32px;" src="/upload/link/' + d.webicon + '"/>';
                }
            },
            {field: 'webname', title: '网站名称', align: "left"},

            {
                field: 'url', title: '链接地址', align: 'left', templet: function (d) {
                    return '<a class="layui-blue" href="' + d.url + '" target="_blank">' + d.url + '</a>'
                }
            },
            {field: 'webmaster', title: '站长', align: 'left'},
            {
                field: 'status', title: '状态', align: 'center', templet: function (d) {
                    if (d.status == '0') {
                        return '待审核';
                    } else if (d.status == '1') {
                        return '通过';
                    } else if (d.status == '2') {
                        return '否决';
                    }
                }
            },
            {title: '操作', templet: '#linkListBar', fixed: "right", align: "center"}
        ]]
    });


    //添加文章
    function addLink() {
        var index = layui.layer.open({
            title: "添加友链",
            type: 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area: ["450px", "500px"],
            content: "/manage/link/add",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }


    $(".add_btn").click(function () {
        addLink();
    });

    function changeStsLink(data, sts) {
        var id = data.id;
        var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        $.ajax({
            type: "post",
            url: "/manage/link/status",
            dataType: "json",
            data: {
                id: id,
                status: sts
            },
            success: function (result) {
                if (result.flag == "success") {
                    top.layer.msg("操作成功！", {icon: 1});
                    tableIns.reload();
                    layer.close(index);
                } else {
                    layer.msg("操作失败", {icon: 2});
                }
            }
        });
    }

    //列表操作
    table.on('tool(linkList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        if (layEvent === 'agree') { //编辑
            changeStsLink(data, 1);
        } else if (layEvent === 'disagree') {
            changeStsLink(data, 2);
        } else if (layEvent === 'del') {
            layer.confirm('确定删除此友链吗？', {icon: 3, title: '提示信息'}, function (index) {
                $.post("/manage/link/delete", {
                    id: data.id  //将需要删除的newsId作为参数传入
                }, function (data) {
                    data = JSON.parse(data);
                    if (data.flag === "success") {
                        layer.msg("删除成功", {icon: 1});
                        tableIns.reload();
                        layer.close(index);
                    } else {
                        layer.msg("删除失败！", {icon: 2});
                    }
                })
            });
        }
    });
});